<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-button type="primary" @click="addialog()">添加分类</el-button>

    <el-dialog title="添加商品" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="form.cat_name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="form">
        <el-form-item label="父级分类" :label-width="formLabelWidth">
          <el-cascader
            :options="options"
            :props="props"
            @change="allcate"
            v-model="val"
          ></el-cascader>
          {{ val }}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addcfl">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%" row-key="cat_id">
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="cat_name" label="分类名称"> </el-table-column>
      <el-table-column prop="cat_deleted" label="是否有效">
        <template slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="!scope.row.cat_deleted"
            style="color: green"
          >
          </i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
      </el-table-column>
      <el-table-column prop="cat_level" label="分类排序">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag v-if="scope.row.cat_level == 1" type="success">二级</el-tag>
          <el-tag v-if="scope.row.cat_level == 2" type="warning">三级</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="del(scope.row)"
          ></el-button>

          <el-button size="small" icon="el-icon-edit"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[1, 3, 5, 7]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
import { cateList, addcate, towlist, delCate } from "../../utils/api";
export default {
  data() {
    return {
      dialogTableVisible: false,
      dialogFormVisible: false,
      val: [],
      // 用于存放表格数据
      // 商品分类的数据列表，默认为空
      tableData: [] ,
      // 父级分类数据
      options: [],
      // 将数据渲染到级联选择器 定义一个属性用来存放
      props: {
        value: "cat_id", // 绑定自己的数据
        label: "cat_name",
        expandTrigger: "hover", //次级菜单的展开方式
      },
      form: {
        cat_name: "",
      },
      queryInfo: {
        query: "", //当前页数
        pagenum: 1, // 每页显示多少数据
        pagesize: 5,
      },
      total: 0,
      formLabelWidth: "120px",
    };
  },

  mounted() {
    this.getlist();
    this.gettow();
  },
  methods: {
    async getlist() {
      var res = await cateList(this.queryInfo);                                    
      if (res.data.meta.status == 200) {
        this.$message.success(res.data.meta.msg);
      }
      this.tableData = res.data.data.result;
      this.total = res.data.data.total;
    },
    //  点击按钮出现 对话框
    addialog() {
      this.selectedKeys = [];
      this.dialogFormVisible = true;
      this.getlist();
    },

    // 练级选择器事件
    allcate(val) {
      console.log(333333, val);
      this.form.cat_level = val.length;
      this.form.cat_id = val[val.length - 1];
    },

    // 二层三级选择器数据
    async gettow() {
      var res = await towlist();
      console.log(44444, res);
      this.options = res.data.data;
    },

    // 点击确定添加商品分类
    async addcfl() {
      var obj = {
        cat_pid: this.val[this.val.length - 1],
        cat_name: this.form.cat_name,
        cat_level: this.val.length,
      };
      var res = await addcate(obj);
      this.dialogFormVisible = false;
      this.getlist();
    },

    //  点击删除
async del(res) {
    console.log(555,res);
      var res = await delCate(res.cat_id)
      console.log(777,res);
       this.getlist();
    },
    // 分页
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getlist();
      this.val = "";
    },
  },
};
</script>